<?php
define('SUB_DIR','/services');
$actions = array('get_auctions','get_auction','bid');
if(isset($_REQUEST['action']))
{
	$action = strtolower($_REQUEST['action']);
	
	if(!in_array($action,$actions))
		exit;
}
else
	exit;

require '../core/service/fanwe.service.php';

if($action == 'bid')
{
	$fanwe = &FanweService::instance();
	$fanwe->cache_list = array();
	$fanwe->is_cron = false;
	$fanwe->is_session = false;
	$fanwe->initialize();
	
	global $_FANWE;
	$result = array('is_error'=>false,'message'=>'');
	
	if($_FANWE['uid'] < 1)
	{
		$result['is_error'] = true;
		resultJson($result);
	}
	
	$id = intval($_FANWE['request']['id']);
	if($id < 1)
	{
		$result['is_error'] = true;
		resultJson($result);
	}
	
	if($_FANWE['group_id'] != 2)
	{
		$result['is_error'] = true;
		$result['message'] = '推广竞拍只针对"<span>团购网站</span>"用户组；<br/>你可以点击&nbsp;“<a href="'.u("site/submit").'" target="_blank">提交网站</a>”&nbsp;进行注册。';
		resultJson($result);
	}
	
	$auction = S('auction')->getItem($id);
	if(!$auction)
	{
		$result['is_error'] = true;
		resultJson($result);
	}
	
	if($auction['auction_status'] == 1)
	{
		$result['is_error'] = true;
		resultJson($result);
	}
	
	if($auction['end_time'] > 0 && TIME_UTC - $auction['end_time'] >= 1)
	{
		$result['is_error'] = true;
		resultJson($result);
	}
	
	$ratio = S('bbs')->getECRatio();
	$money = S('User')->getUserScore($_FANWE['uid']) / $ratio;
	$freeze_money = S('auction')->getFreezeMoney($id,$_FANWE['uid']);
	$money += $freeze_money;
		
	if($auction['pre_pay'] > $money)
	{
		$result['is_error'] = true;
		$result['message'] = '余额不足，该竞拍需要冻结预付费&nbsp;<span>'.$auction['pre_pay_format'].'</span>；<br/>你可以进入会员管理中心&nbsp;“<a href="'.U('site/center').'" target="_blank">积分充值</a>”&nbsp;补充余额。';
		resultJson($result);
	}
	
	$auction_status = 0;
	
	if($auction['buyout_price'] > 0)
	{
		if(intval($_FANWE['request']['is_buyout']) == 1)
		{
			$bid_price = $auction['buyout_price'];
			$auction_status = 1;
		}
		else
		{
			$bid_price = S('auction')->getAuctionPrice($id);
			$bid_price += $auction['amplitude'];
			
			if($bid_price > $auction['buyout_price'])
			{
				$bid_price = $auction['buyout_price'];
				$auction_status = 1;
			}
		}
	}
	else
	{
		$bid_price = S('auction')->getAuctionPrice($id);
		$bid_price += $auction['amplitude'];
	}
	
	if($money < $bid_price)
	{
		$result['is_error'] = true;
		$result['message'] = '余额不足，该竞拍需要冻结余额&nbsp;<span>¥'.floatval($bid_price).'</span>；<br/>你可以进入会员管理中心&nbsp;"<a href="'.U('site/center').'" target="_blank">积分充值</a>"&nbsp;补充余额。';
		resultJson($result);
	}
	
	require_once import('class/iplocation');
	$ip_location = new IpLocation();
	$ip_address = $ip_location->getAddress($_FANWE['client_ip']);
	$ip_area = $ip_address['area1'];
	
	$log = array();
	$log['auction_id'] = $id;
	$log['user_id']    = $_FANWE['uid'];
	$log['auction_price'] = $bid_price;
	$log['auction_time'] = TIME_UTC;
	$log['auction_ip'] = $_FANWE['client_ip'];
	$log['auction_addr'] = $ip_area;
	
	if(S('auction')->insertAuctionLog($log))
	{
		$update_auction = array();
		$auction['user_id'] = $_FANWE['uid'];
		$update_auction['user_id'] = $_FANWE['uid'];
		
		if($auction['end_time'] > 0 && $auction['end_time'] - TIME_UTC <= intval($_FANWE['setting']['auction_end_time']))
		{
			$auction['end_time'] += intval($_FANWE['setting']['auction_add_time']);
			$auction['end_time_format']  = toDate($auction['end_time']);
			$auction['end_time_js']  = toDate($auction['end_time'],'r');
			$update_auction['end_time'] = $auction['end_time'];
		}
		
		DB::query('UPDATE '.DB::table('auction_bid')." SET bid_price = '$bid_price' WHERE auction_id = '$id'");
		
		$money1 = $bid_price;
		if($auction['pre_pay'] > $money1)
			$money1 = $auction['pre_pay'];
		
		if($auction_status == 1)
		{
			$auction['auction_status'] = 1;
			$update_auction['auction_status'] = 1;
		}
		else
			DB::query('UPDATE '.DB::table('auction')." SET user_id = '$_FANWE[uid]' WHERE id = '$id'");
		
		DB::update('auction',$update_auction,"id = '$id'");
		S('auction')->setAuction($auction);
		S('auction')->setFreezeMoney($id,$_FANWE['uid'],$money1,$freeze_money);
		
		$bid_info['uid'] = $_FANWE['uid'];
		$bid_info['username'] = $_FANWE['user_name'];
		$bid_info['price'] = $bid_price;
		$bid_info['bbs_price'] = $bid_price * $ratio;
		$bid_info['time'] = TIME_UTC;
		$bid_info['time_format'] = toDate(TIME_UTC);
		S('auction')->setAuctionBid($id,$bid_info);
		
		if($auction_status == 1)
			S('auction')->auctionComplete($auction);

		$user_ip = preg_replace("/(.*)\..*?\..*?$/i","\\1.*.*",$_FANWE['client_ip']);
		$bid_info['auction_ip'] = $user_ip;
		$bid_info['auction_addr'] = $ip_area;
		S('auction')->setAuctionRecords($id,$bid_info);
		S('auction')->setAuctionRanks($id,$bid_info);
	}
	else
	{
		$result['is_error'] = true;
		$result['message'] = '对不起，您的这次竞拍发生错误，请联系管理员。';
		resultJson($result);
	}
	
	resultJson($result);
}
elseif($action == 'get_auction')
{
	$fanwe = &FanweService::instance();
	$fanwe->cache_list = array();
	$fanwe->is_cron = false;
	$fanwe->is_session = false;
	$fanwe->initialize();
	
	global $_FANWE;
	
	$result = array();
	
	$id = intval($_FANWE['request']['id']);
	if($id < 1)
		exit;
		
	$auction = S('auction')->getItem($id);
	if(!$auction)
		exit;
		
	if($auction['end_time'] > 0 && TIME_UTC > $auction['end_time'] && $auction['auction_status'] == 0)
	{
		$auction['auction_status'] = 1;
		DB::query('UPDATE '.DB::table('auction')." SET auction_status = 1 WHERE id = '$id'");
		S('auction')->setAuction($auction);
		S('auction')->auctionComplete($auction);
	}
	
	$result['uid'] = 0;
	
	if($_FANWE['uid'] > 0)
	{
		$ratio = S('bbs')->getECRatio();
		$result['uid'] = $_FANWE['uid'];
		$result['money'] = S('User')->getUserScore($_FANWE['uid']) / $ratio;
		$result['freeze_money'] = floatval(S('auction')->getFreezeMoney($id,$_FANWE['uid'],true));
	}
	
	
	$result['end_time_js'] = $auction['end_time_js'];
	$result['end_time_format'] = $auction['end_time_format'];
	$result['auction_status'] = $auction['auction_status'];
	$result['bid'] = S('auction')->getAuctionBid($id);
	$result['records'] = S('auction')->getAuctionRecords($id);
	$result['ranks'] = S('auction')->getAuctionRanks($id);
	
	resultJson($result);
}
elseif($action == 'get_auctions')
{
	$fanwe = &FanweService::instance();
	$fanwe->cache_list = array();
	$fanwe->is_user = false;
	$fanwe->is_cron = false;
	$fanwe->is_session = false;
	$fanwe->initialize();
	
	global $_FANWE;
	$result = array();
	
	$ids = trim($_FANWE['request']['ids']);
	if(empty($ids))
		exit;
	
	$ids = explode(',',$ids);
	foreach($ids as $id)
	{
		$id = intval($id);
		if($id > 0)
		{
			$auction = S('auction')->getItem($id);
			if(!$auction)
				continue;
				
			if($auction['end_time'] > 0 && TIME_UTC > $auction['end_time'] && $auction['auction_status'] == 0)
			{
				$auction['auction_status'] = 1;
				DB::query('UPDATE '.DB::table('auction')." SET auction_status = 1 WHERE id = '$id'");
				S('auction')->setAuction($auction);
				S('auction')->auctionComplete($auction);
			}
			
			$result['auction_'.$id]['bid'] = S('auction')->getAuctionBid($id);
			$result['auction_'.$id]['end_time_js'] = $auction['end_time_js'];
			$result['auction_'.$id]['end_time_format'] = $auction['end_time_format'];
			$result['auction_'.$id]['auction_status'] = $auction['auction_status'];
		}
	}
	
	resultJson($result);
}

function resultJson($result)
{
	include import('class/json');
	$json = new JSON();
	die($json->encode($result));
	exit;
}
?>